/*
 * Created on 06.02.2004
 * Local Projects Database
 * Mihai Postelnicu (mihai@ro-gateway.org)
 * (c) 2003 by eRomania Gateway
 */
package ro.gateway.aida.obj.db;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

import ro.gateway.aida.db.DBPersistenceManager;
import ro.gateway.aida.db.DBWrapable;
import ro.gateway.aida.db.PersistenceToken;
import ro.gateway.aida.obj.BreadCrumb;
import ro.gateway.aida.servlet.Constants;
import ro.xblue.translator.TransItem;


/**
 * @author Mihai Postelnicu<p>
 *
 *  e-mail (mihai@ro-gateway.org)<br>
 * (c) 2003 by eRomania Gateway<p>
 */
public class BreadCrumbDB extends DBPersistenceManager implements DBWrapable
{
    private BreadCrumbDB(PersistenceToken token) {
        super(token);
    }

    public static BreadCrumbDB getManager(PersistenceToken token) {
        return new BreadCrumbDB(token);
    }

    public void synchronize(Object o) throws Exception {
    }

    public void delete(Object o) throws Exception {
    }

    public Object get(Object o) throws Exception {
        return getAll();
    }

    public ArrayList getAll() throws Exception {
        ArrayList ret = new ArrayList();
        Connection con = getConnection();

        try {
            PreparedStatement ps = con.prepareStatement(GET_ALL);

            ResultSet rs = ps.executeQuery();

            while (rs.next()) {
            	if(!"BreadCrumbs".equals(rs.getString(7)) && rs.getString(7)!=null) continue;
                BreadCrumb bc = new BreadCrumb();
                TransItem item = new TransItem();
                
                item.setModule("BreadCrumbs");
                item.setId(rs.getLong(1));
                item.setLabel(rs.getString(3));
             
                item.setLang(rs.getString(4));
                if(rs.getString(5)==null) {
                	item.setText(item.getLabel());
                	item.setLang(Constants.DEFAULT_LANGUAGE);
                } else item.setText(rs.getString(5));
                
                bc.setLabel(item.getLabel());
                bc.setParentLabel(rs.getString(2));
                bc.setTrn(item);
                bc.setUrl(rs.getString(6));
                ret.add(bc);
            }

            rs.close();
            ps.close();
        } finally {
            con.close();
        }

        return ret;
    }

    public static final String GET_ALL =
        "SELECT ift.id,b.parentlabel,b.label,ift.lang,ift.text,b.url,ift.module FROM breadcrumbs b LEFT OUTER JOIN iface_trans ift ON (b.label=ift.label) ";
       
}
